<?php
  /**
   * iJRemoteCall
   *
   * The iJRemoteCall class provides calling progect methods
   *
   * @copyright Copyright (c) 2011, iJBlocks Progect
   */
class iJRemoteCall {
    
    /**
     *
     * Class Constructuctor
     * 
     * @access public
     */
    public function __construct() {

    }
    
    /**
     *
     * executing ajax reuest
     * 
     * @param JSON $requset - request data
     * 
     * @return calling method result, or null if request failed
     * 
     * @access public
     */
    public static function executeAJAXRequest($requset) {
     
      if(isset($requset['JSON']) && isset($requset['className']) && isset($requset['methodName'])) {
         $parameters = json_decode($requset['JSON'], true);
         if(!is_array($parameters)) {
             $parameters = json_decode($parameters);
         }
         $class =  $requset['className'];
         $method = $requset['methodName'];
       
         
         if(!empty($parameters)) {
            $result      = null;
            $params_list = '';
            $params      = array();
            for ($i = 0, $n=count($parameters); $i < $n; $i++) {
              $params[] = '$parameters[' . $i . ']';
            }
  
            $params_list = implode(',', $params);
         }
        
         eval("\$result = iJFactory::call('" . $class . "', '" . $method . "'" . (!empty($params_list)?','. $params_list:'') . ");");
         
         echo json_encode($result);

      } else {
        return null;
      }
      
    }
}
?>